package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;

import android.app.Activity;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.QwertyKeyListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class User_List extends ListActivity{
	
	public static DBAdapter HnopenHelper;
	public static SQLiteDatabase sdb;
	private DBAdapter HopeNoteDB;
	private SQLiteDatabase db;
	private String title, qWhere;
	private static String str_user;
	public static TextView tv_name, tv_age, tv_sex;
	public static	ArrayList<String> lst_item, lst_value;
	public static ListView lv_item;
	private	static ArrayList<User_Data> User_item;
	private static User_List_Adapter U_Adater;
	public static Cursor cursor;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_list);
		
		IntentFilter filter = new IntentFilter("UserListReset");
		registerReceiver(BR_UserList,filter);
		
		HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();
        tv_name = (TextView)findViewById(R.id.row_name);
        tv_age = (TextView)findViewById(R.id.row_age);
        tv_sex = (TextView)findViewById(R.id.row_sex);
        //lv_item = (ListView)findViewById(R.id.lv_item); 
        
        //qWhere = HopeNoteDb.COL_GROUP + " = '" + str_group + "'";
    	cursor = sdb.query(HopeNoteDb.TB_USER, new String[] {HopeNoteDb.COL_USER,HopeNoteDb.COL_SEX,HopeNoteDb.COL_AGE}, null, null, null, null, null);
    	
    	cursor.moveToFirst();
        User_item = new ArrayList<User_Data>();
        User_Data datarow;
        for(int i=0; i < cursor.getCount(); i++)
        {
        	datarow = new User_Data(cursor.getString(0),cursor.getString(1),cursor.getString(2));
        	User_item.add(datarow);
        	cursor.moveToNext();
        }
        cursor.close();
        U_Adater = new User_List_Adapter(User_List.this, R.layout.row, User_item);
        //lv_item.setAdapter(U_Adater);
        setListAdapter(U_Adater);
        
        
    }

	@SuppressWarnings("unused")
	@Override
	protected void onListItemClick(ListView l, View v, int position, long data) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, data);
		
		String title = ((User_Data)l.getItemAtPosition(position)).user_name;
		
		
		//title = title.substring(0,3);
		
		HopeNoteDb.CURRENT_USER = title;
		
		
		//broadcast
		//if(getParent().getClass().equals(Note_Search_Tab.class))
		//{
			Intent intent = new Intent("Reset");
			sendBroadcast(intent);
			
			Intent intent_main = new Intent("BR_Main");
			sendBroadcast(intent_main);
			
			Intent intent_noteadd = new Intent("BR_NoteAdd");
			sendBroadcast(intent_noteadd);
		//}
		//else
		//{
		//	Intent intent = new Intent();
		//	intent.setAction("Note_Input_btSet");
		//	sendBroadcast(intent);
		//}
		
			
		
		SharedPreferences cache = getSharedPreferences("cache", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = cache.edit();
		editor.putString("USER", title);
		editor.commit();
		
		finish();
		
		
	}
	
	private BroadcastReceiver BR_UserList = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			U_Adater.notifyDataSetChanged();
		}
	};
	
	
}
	

	
	
	/*@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_list);
		ArrayList<String> data = new ArrayList<String>(); 

		//데이터베이스 열기
		HopeNoteDB = new DBAdapter(getApplicationContext());
		db = HopeNoteDB.getReadableDatabase();
		
		
		//반환할 컬럼 항목
		String[] getColumns = {HopeNoteDb.COL_USER, HopeNoteDb.COL_SEX, HopeNoteDb.COL_AGE};
		//데이터베이스 쿼리
		Cursor Result = db.query(HopeNoteDb.TB_USER,getColumns, null, null, null, null, null);
		Result.moveToFirst();
		
		while(!Result.isAfterLast()){
			
			String user = Result.getString(0);
			String sex = Result.getString(1);
			String age = Result.getString(2);
			data.add(user+sex+age);
			Result.moveToNext();
			Toast.makeText(this,user+sex+age ,Toast.LENGTH_SHORT).show();
		}
		Result.close();
		
		
		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
		//setListAdapter(new SimpleCursorAdapter(this, R.layout.row,Result,FROM,TO));
		//data.get(getSelectedItemPosition());
		//Result.getPosition();
		//onListItemClick(getListView(), getListView(), , Result);
		
		
	}*/